#!/bin/bash

REPO_ROOT=$(git rev-parse --show-toplevel)

# Find sdbus++-gen-meson
#   1. Check $SDBUSPP_GEN_MESON
#   2. Check in $PATH
#   3. Check in subprojects/sdbusplus
if [ ! -x "$SDBUSPP_GEN_MESON" ]; then
    SDBUSPP_GEN_MESON="$(which sdbus++-gen-meson 2> /dev/null)"
fi
if [ ! -x "$SDBUSPP_GEN_MESON" ]; then
    SDBUSPP_GEN_MESON="$REPO_ROOT/subprojects/sdbusplus/tools/sdbus++-gen-meson"
fi
if [ ! -x "$SDBUSPP_GEN_MESON" ]; then
    echo "Cannot find sdbus++-gen-meson ($SDBUSPP_GEN_MESON)."
    exit 1
fi

echo "Generating meson files..."
$SDBUSPP_GEN_MESON \
    --command meson \
    --directory "$REPO_ROOT/yaml" \
    --output "$REPO_ROOT/gen"

echo "Formatting meson files..."
find "$REPO_ROOT/gen" -name "meson.build" -exec meson format -i {} +
